package com.styytech.dadabao.ui.more;


import android.annotation.SuppressLint;
import android.content.Context;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.RegistResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.bean.MyMessage;
import com.styytech.dadabao.bean.Notice;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.utils.CommonUtils;

/**
 * 服务公告详情
 */
@ContentView(R.layout.activity_notice_details)
public class NoticeDetailsActivity extends BaseActivity {

    /*标题*/
    @ViewInject(R.id.tv_notice_title)
    TextView tv_notice_title;
    /*内容*/
    @ViewInject(R.id.tv_notice_content)
    TextView tv_notice_content;
    /*内容html*/
    @ViewInject(R.id.wb_notice_content)
    WebView wb_notice_content;
    /*发布时间*/
    @ViewInject(R.id.tv_notice_time)
    TextView tv_notice_time;

    /*上下文*/
    private Context mContext;
    private int userMessageId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        initView();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("服务公告", View.VISIBLE);

    }

    /**
     * 初始界面设置
     */
    private void initData() {
        Object obj = getIntent().getSerializableExtra("BEAN");
        if (obj instanceof Notice) {
            Notice notice = (Notice) obj;
            tv_notice_title.setText(notice.getTitle());
            tv_notice_time.setText(notice.getPublishTime());
            openWeb(notice.getContent());
        } else if (obj instanceof MyMessage) {
            tv_notice_content.setVisibility(View.VISIBLE);
            wb_notice_content.setVisibility(View.GONE);
            setTopBar("站内通知", View.VISIBLE);
            MyMessage message = (MyMessage) obj;
            userMessageId = message.getUserMessageId();
            tv_notice_title.setText(message.getTitle());
            tv_notice_time.setText(message.getCreateTime());
            tv_notice_content.setText(message.getMessageContent());
            requestSetMessageStatus();
        }


    }

    /**
     * 打开网页
     */
    @SuppressLint("SetJavaScriptEnabled")
    public void openWeb(String content) {

        WebSettings webSettings = wb_notice_content.getSettings();
        // 能够的调用JavaScript代码
        webSettings.setJavaScriptEnabled(true);
        /*允许加载网络上的图片资源*/
        webSettings.setBlockNetworkImage(false);
        wb_notice_content.getSettings().setLayoutAlgorithm(
                WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wb_notice_content.loadDataWithBaseURL(null, content,
                "text/html", "utf-8", null);
//        wb_notice_content.loadUrl("https://www.dadabao.net//ueditor/jsp/upload/image/20150817/1439779983218013068.jpg");
//        wb_notice_content.loadUrl("http://www.shstzz.com/ydimages/001.jpg");
        wb_notice_content.setWebChromeClient(new WebChromeClient());
        wb_notice_content.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                handler.cancel(); //默认的处理方式，WebView变成空白页
                /*接受https协议*/
                handler.proceed();//接受证书
//                handleMessage(Message msg); //其他处理
            }
        });
    }

    private void requestSetMessageStatus() {

        /*请求参数*/
        RequestParams params = new RequestParams();
        params.put("userMessageId", userMessageId);
        params.put("status", 1);
        /*提交请求*/
        submitRequest(params);
    }

    /**
     * 提交审核请求
     *
     * @param params 请求参数
     */
    private void submitRequest(RequestParams params) {
        String url = ConstantsServerUrl.getUpdateUserMessageStatusUrl();// 获得用户账户绑定银行卡列表接口
        //获取验证码
        RegistResult request = new RegistResult(responseResult);
        try {
            new HttpRequest(this).doPost(request, url, params);
        } catch (ArgumentsException e) {
            Log.i("INFO", "1e:" + e);
            e.printStackTrace();
        } catch (RequestException e) {
            Log.i("INFO", "2e:" + e);
            e.printStackTrace();
        }
    }

    /**
     * 提交请求结果处理
     */
    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
//            Toast.makeText(mContext,"已读",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void executeError(String message, int errorCode) {
            setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "" + message);
            CommonUtils.showToastMsg(context, "" + message);
        }

        @Override
        public void HttpError(String message, int errorCode) {
            setView(View.VISIBLE, View.GONE, View.VISIBLE, View.VISIBLE, "" + message);
            CommonUtils.showToastMsg(context, "" + message);
        }
    };

}

